//
//  ByViewStoreProductListTable.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/6.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewStoreProductListTable.h"


// cell
#import "ByCellShopProduct.h"               // 商品cell

// view
#import "ByViewShopProductSort.h"           // 排序

@interface ByViewStoreProductListTable() <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView       *table;

@end

@implementation ByViewStoreProductListTable
{
    NSInteger _items;
}

- (void)setupUI {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self addSubview:self.table];
    
    [self.table registerClass:[ByCellShopProduct class] forCellReuseIdentifier:@"productCell"];
    
}
- (void)setFrame:(CGRect)frame {
    
    [super setFrame:frame];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}
- (void)setIndex:(NSInteger)index {
    
    _index = index;
    
    __weak typeof(self) wself = self;
    if (_index == 0) {
        self.contentViewDidAppear = ^(NSInteger index) {
            
            _items = arc4random_uniform(5) + 1;
            [wself.table reloadData];
        };
    }
    else {
        self.contentViewDidAppear = ^(NSInteger index) {
            
            _items = arc4random_uniform(5) + 6;
            [wself.table reloadData];
        };
    }
    
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _items;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellShopProduct *cell = [tableView dequeueReusableCellWithIdentifier:@"productCell"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return (SCREEN_WIDTH - 20) / 2 + 40 + 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 40 + 10;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *header = [UIView new];
        
        ByViewShopProductSort *sortView = [ByViewShopProductSort new];
        [header addSubview:sortView];
        
        UIView *marginView = [UIView new];
        [header addSubview:marginView];
        
        [sortView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(header).insets(UIEdgeInsetsMake(0, 0, 10, 0));
        }];
        
        return header;
    }
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}

@end
